<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
include_once($phpbb_root_path . 'ofc/php-ofc-library/open-flash-chart.' . $phpEx);

// Basic parameter data
$mode		= request_var('mode', '');

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];


if ($mode == 'query_user')
{
	$query_user_id	= (int)(request_var('user_id', ''));
	
	// list all holding stock.
	list_holding_stock($query_user_id);
	
	// list all delegation. 
	$sql 	= 'SELECT * FROM sv_transaction WHERE user_id=' . $query_user_id . ' ORDER BY request_time DESC';
	$result = $db->sql_query($sql); 
	while ($row = $db->sql_fetchrow($result))
	{
		if (is_valid_corp_id($row['corp_id']) == false)
		{
			continue;
		}
	
		$list = true;
	
		$deal_time = 0;
		if ((int)$row['deal_time'] != 0)
		{
			$deal_time = date('Y.m.d H:i', (int)$row['deal_time']);
		}
	
		// Send vars to template
		$template->assign_block_vars('list', array(		
			'CORP_ID'						=> $row['corp_id'],
			'CORP_NAME'						=> get_corp_chinese_name($row['corp_id']),
			'NUMBER'						=> (int)$row['share']/1000,
			'ACTION'						=> $row['action'],
			'REQUEST_TIME'					=> date('Y.m.d H:i', (int)$row['request_time']),
			'PRICE'							=> $row['price'],
			'DEAL_TIME'						=> $deal_time,
//			'NOTE'							=> $row['note'],
			)
		);
	}
	
	// list all equity changes. 
	$equity_change_list = false;
	
	$sql 	= 'SELECT * FROM sv_equity_change_log WHERE user_id=' . $query_user_id . ' ORDER BY execution_time DESC';
	$result = $db->sql_query($sql); 
	while ($row = $db->sql_fetchrow($result))
	{	
		$equity_change_list = true;
	
		$execution_time = date('Y/m/d H:i', (int)$row['execution_time']);

		$equity_change_event_id 			= $row['equity_change_event_id'];
		$corp_id 							= $row['corp_id'];
		$share 								= $row['share'];
		$user_get_cash_after_change 		= $row['cash_after_change'];
		$user_get_corp_id_after_change 		= $row['corp_id_after_change'];
		$user_get_share_after_change 		= $row['share_after_change'];
		$user_get_corp_id_after_change_2 	= $row['corp_id_after_change_2'];
		$user_get_share_after_change_2 		= $row['share_after_change_2'];
		$user_get_corp_id_after_change_3 	= $row['corp_id_after_change_3'];
		$user_get_share_after_change_3 		= $row['share_after_change_3'];
		
		// Get equity change event detail. 
		$sql2 	= 'SELECT * FROM sv_equity_change_event WHERE equity_change_event_id=' . $equity_change_event_id;
		$result2 = $db->sql_query_limit($sql2, 1); 
		$row2 = $db->sql_fetchrow($result2);
	
		$share_unit				= $row2['share_unit'];
		$cash_after_change 		= $row2['cash_after_change'];
		$corp_id_after_change 	= $row2['corp_id_after_change'];
		$share_after_change 	= $row2['share_after_change'];
		$corp_id_after_change_2 = $row2['corp_id_after_change_2'];
		$share_after_change_2 	= $row2['share_after_change_2'];
		$corp_id_after_change_3 = $row2['corp_id_after_change_3'];
		$share_after_change_3 	= $row2['share_after_change_3'];
		$scheduled_date 		= $row2['scheduled_date'];
		$execution_date 		= $row2['execution_date'];
	
	
		// Send vars to template
		$template->assign_block_vars('equity_change_log_list', array(		
			'CORP_ID'							=> $corp_id,
			'CORP_NAME'							=> get_corp_chinese_name($corp_id),
			'SHARE'								=> $share,
			'SHARE_UNIT'						=> $share_unit,

			'USER_GET_CASH_AFTER_CHANGE'		=> $user_get_cash_after_change,
			'USER_GET_CORP_ID_AFTER_CHANGE'		=> $user_get_corp_id_after_change,
			'USER_GET_SHARE_AFTER_CHANGE' 		=> $user_get_share_after_change,
			'USER_GET_CORP_ID_AFTER_CHANGE_2'	=> $user_get_corp_id_after_change_2,
			'USER_GET_SHARE_AFTER_CHANGE_2' 	=> $user_get_share_after_change_2,
			'USER_GET_CORP_ID_AFTER_CHANGE_3'	=> $user_get_corp_id_after_change_3,
			'USER_GET_SHARE_AFTER_CHANGE_3' 	=> $user_get_share_after_change_3,		
		
			'CASH_AFTER_CHANGE'					=> $cash_after_change,
			'CORP_ID_AFTER_CHANGE'				=> $corp_id_after_change,
			'CORP_NAME_AFTER_CHANGE'			=> get_corp_chinese_name($corp_id_after_change),
			'SHARE_AFTER_CHANGE' 				=> $share_after_change,
			'CORP_ID_AFTER_CHANGE_2'			=> $corp_id_after_change_2,
			'CORP_NAME_AFTER_CHANGE_2'			=> get_corp_chinese_name($corp_id_after_change_2),
			'SHARE_AFTER_CHANGE_2' 				=> $share_after_change_2,
			'CORP_ID_AFTER_CHANGE_3'			=> $corp_id_after_change_3,
			'CORP_NAME_AFTER_CHANGE_3'			=> get_corp_chinese_name($corp_id_after_change_3),
			'SHARE_AFTER_CHANGE_3' 				=> $share_after_change_3,
			'SCHEDULED_DATE' 					=> get_date_string($scheduled_date),
			'EXECUTION_DATE'					=> get_date_string($execution_date),
		
			)
		);
	}

	// get number of followers.
	$sql2 = 'SELECT COUNT(DISTINCT follower_user_id) AS num FROM sv_follow_list WHERE followee_user_id=' . $query_user_id;
	$result2 = $db->sql_query($sql2); 
	$row2 = $db->sql_fetchrow($result2);	
	$follower_num = $row2['num'];
		
	// check if this user is a follower of the queried user.
	$following = false;
	$sql2 = 'SELECT * FROM sv_follow_list WHERE followee_user_id=' . $query_user_id . ' AND follower_user_id = ' . $user_id;
	$result2 = $db->sql_query_limit($sql2, 1);
	if ($row2 = $db->sql_fetchrow($result2))
	{
		$following = true;
	}
	
	
	
	// draw user value
	$performance = false;
	draw_user_value($query_user_id);
}


// list ROI for 1, 3, 6, 12 month period
$show_1_month_roi = false;
$show_3_month_roi = false;
$show_6_month_roi = false;
$show_12_month_roi = false;

$sql	= 'SELECT roi, period FROM sv_user_roi WHERE user_id=' . $query_user_id;
$result = $db->sql_query($sql); 
while ($row = $db->sql_fetchrow($result))
{
	switch($row['period'])
	{
		case 30:
			$show_1_month_roi = true;
			$roi_1_month = round($row['roi']*100, 1);		
		break;
		
		case 90:
			$show_3_month_roi = true;
			$roi_3_month = round($row['roi']*100, 1);		
		break;
		
		case 120:
			$show_6_month_roi = true;
			$roi_6_month = round($row['roi']*100, 1);		
		break;
		
		case 360:
			$show_12_month_roi = true;
			$roi_12_month = round($row['roi']*100, 1);		
		break;
		
		default:
		break;
	}	
}

display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_FOLLOW_ACTION'			=> append_sid("{$phpbb_root_path}follow.$phpEx"),
	
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
	'S_LIST'					=> $list,
	'S_HOLDING_STOCK_LIST'		=> $holding_stock_list,
	'U_QUERY_USERNAME'			=> get_username($query_user_id),
	'S_EQUITY_CHANGE_LIST'		=> $equity_change_list,
	
	'S_SHOW_QUERY_USERNAME_IN_HOLDING_STOCK_LIST'	=> true,
	
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '',
	
	'JASON_STRING'			=> $jason_string,
	'S_PERFORMANCE'				=> $performance,
	
	'SHOW_1_MONTH_ROI'	=>	$show_1_month_roi,
	'SHOW_3_MONTH_ROI'	=>	$show_3_month_roi,
	'SHOW_6_MONTH_ROI'	=>	$show_6_month_roi,
	'SHOW_12_MONTH_ROI'	=>	$show_12_month_roi,
	'S_ROI_1_MONTH'		=>	$roi_1_month,
	'S_ROI_3_MONTH'		=>	$roi_3_month,
	'S_ROI_6_MONTH'		=>	$roi_6_month,
	'S_ROI_12_MONTH'	=>	$roi_12_month,
	
	'S_FOLLOWER_NUM'	=> $follower_num,
	'S_FOLLOWING'		=> $following,
	'S_QUERY_USER_ID'	=> $query_user_id,
	'S_USER_ID'			=> $user_id,
	)
);

// Output page
page_header($user->lang['SV_QUERY_USER']);

$template->set_filenames(array(
	'body' => 'query_user.html')
);

page_footer();

?>